<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang@fistforward.com
// +----------------------------------------------------------------------

/**
 * 推广URL类
 * @package  htdocs 
 * @subpackage  quanliadmin
 * @author   allen.qiang@fistforward.com
 * @version 1.0
 */

class sysPromote{


    /**
     * 获取推广url信息
     * @param $arg_array
     * @return array|null
     */
    public function getPromoteList(&$arg_array){
    	global $G_PGLOB;
        //sql字符串
        $sql = '';
        
        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //KEY1
        if (isset($arg_array['key1']) && is_numeric($arg_array['key1']) && $arg_array['key1']>0) {
            $sql .= ' AND key1 = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['key1'];
        }

        //KEY2
        if (isset($arg_array['key2']) && !empty($arg_array['key2'])) {
            $sql .= ' AND key2 like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['key2'].'%';
        }

        //KEY3
        if (isset($arg_array['key3']) && !empty($arg_array['key3'])) {
            $sql .= ' AND key3 like ?';
            $params_type .= 's';
            $params_array[] = '%'.$arg_array['key3'].'%';
        }
		
        //区域类型
        if (isset($arg_array['url']) && !empty($arg_array['url'])) {
            $sql .= ' AND url = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['url'];
        }
        $sql .= ' AND status = ?';
        $params_type .= 'i';
        $params_array[] = 1;
       
        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4) ;
        }



        try {

            if (isset($arg_array['rp'])) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM sys_promote  ' . $sql;
                $count_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }


            //查询分类信息
            $search_sql = 'SELECT * FROM sys_promote ' . $sql;
            $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);
          
            if (!$temp_item_array)
                return null;

            $item_array = array();

            foreach($temp_item_array as &$item) {
            	
                $item_array[] = array(
                    'key1' => $item->key1,
                    'key2' => $item->key2,
                    'key3' => $item->key3,
                    'url' => $item->url,
                    //mod by allen.qiang 优化 start
                    'start_time' => $item->start_time,
                    'end_time' => $item->end_time,
                    //mod by allen.qiang 优化 end
                	'creator' => $item->creator,
                	'createTime' => $item->createTime,
                	'uuid'=>$item->uuid	,
                	'promoteID' => $item->promoteID,
                	'complete_url'=>$item->complete_url	
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }


    }

	//修改推广状态
	public function updateSave($promoteID){
		
		 global $G_PGLOB;
		//根据promoteID把他的状态修改为0
		for($i=0;$i<count($promoteID);$i++){
			$promote =  new entity('sys_promote');
			$promote->promoteID = $promoteID[$i];
			$G_PGLOB->DBCA->fetch($promote);
			$promote->status = 0;
			$G_PGLOB->DBCA->save($promote);
		}
	
		
		return 1;
		
		
	}
	
	
	//判断是否存到log_web中
	public function checkUrl(&$arg_array){
		$key_arr = explode(',',$arg_array['key']);
	    	global $G_PGLOB;
        //sql字符串
        $sql = '';
		
        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //KEY1
        if (isset($key_arr[0]) && is_numeric($key_arr[0]) && $key_arr[0]>0) {
            $sql .= ' AND key1 = ?';
            $params_type .= 'i';
            $params_array[] = $key_arr[0];
        }

        //KEY2
        if (isset($key_arr[1]) && !empty($key_arr[1])) {
            $sql .= ' AND key2 = ?';
            $params_type .= 's';
            $params_array[] = $key_arr[1];
        }

        //KEY3
        if (isset($key_arr[2]) && is_numeric($key_arr[2])) {
            $sql .= ' AND key3 = ?';
            $params_type .= 's';
            $params_array[] = $key_arr[2];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4) ;
        }


       
        try {

            if (isset($arg_array['rp'])) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM log_web  ' . $sql;
                $count_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);

                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

			
            //查询分类信息
            $search_sql = 'SELECT * FROM log_web ' . $sql;
            $temp_item_array = &$G_PGLOB->DBCA->stmt_query($search_sql, $params_type, $params_array);
       
            if (!$temp_item_array)
                return null;

            $item_array = array();

            foreach($temp_item_array as &$item) {
            	
                $item_array[] = array(
                    'key1' => $item->key1,
                    'key2' => $item->key2,
                    'key3' => $item->key3,
                    'access_url' => $item->access_url,
                	'createTime' => $item->createTime,

                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
	
	}	
	
	
	
	
	//判断是否存到log_web中
	public function checkUrl1(&$key){
		global $G_PGLOB;
		//首先把key转换成数组
		$key_arr = explode(',',$key);
		$sql = "select * from log_web where key1 = $key_arr[0] and key2 = '$key_arr[1]' and key3 = '$key_arr[2]' and date(now())";

		$result = $G_PGLOB->DBCA->stmt_query($sql);
		//统计数量
		$key['count'] = count($result);
		return $result;
		
	}

}